/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.MaintainTemplateRelation;
import org.springblade.modules.admin.service.MaintainTemplateRelationService;
import org.springblade.modules.admin.vo.TemplateContentPageVO;
import org.springblade.modules.admin.vo.TemplateContentRelationPageVO;
import org.springblade.modules.admin.vo.TemplateLocationTypeTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Date 14:02 2022/3/14
 * @Description {
 *   任务模板 模板分类 工作项关联接口
 * }
 * @Author lsay
 **/
@RestController
@RequestMapping("/maintain/template/relation")
public class MaintainTemplateRelationController {

	@Autowired
	private MaintainTemplateRelationService templateRelationService;

	/**
	 * 根据任务模板id查询对应分类树
	 * @param templateId
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/tree")
	public R<List<TemplateLocationTypeTreeVO>> tree(Long templateId){
		return R.data(templateRelationService.tree(templateId));
	}

	/**
	 * 根据任务模板分类查询工作项
	 * @param templateId
	 * @param tltId
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/content")
	public R<List<TemplateContentRelationPageVO>> contentList(Long templateId, Long tltId){
		return R.data(templateRelationService.contentList(templateId,tltId));
	}

	/**
	 * 新增模板分类关联
	 * @param templateRelation
	 * @return
	 */
	@PostMapping("/location/type/save")
	public R<Boolean> locationTypeSave(@RequestBody MaintainTemplateRelation templateRelation){
		int count = templateRelationService.count(Wrappers.<MaintainTemplateRelation>lambdaQuery()
			.eq(MaintainTemplateRelation::getTemplateId,templateRelation.getTemplateId()).eq(MaintainTemplateRelation::getTltId,templateRelation.getTltId()));
		if(count > 0){
			throw new ServiceException("分类已被添加，请勿重复操作");
		}
		return R.status(templateRelationService.saveOrUpdate(templateRelation));
	}

	/**
	 * 删除模板分类关联
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/location/type/remove")
	public R<Boolean> locationTypeRemove(String ids,Long templateId){
		return R.status(templateRelationService.removeTemplateType(ids,templateId));
	}

	/**
	 * 新增工作项关联
	 * @param templateRelation
	 * @return
	 */
	@PostMapping("/content/save")
	public R<Boolean> contentSave(@RequestBody MaintainTemplateRelation templateRelation){
		return R.status(templateRelationService.contentSave(templateRelation));
	}

	/**
	 * 删除模板分类关联
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/content/remove")
	public R<Boolean> contentRemove(String ids,Long templateId){
		return R.status(templateRelationService.removeContent(templateId,Func.toLongList(ids)));
	}



}
